package khmt.lvtn.dhbk.model.process;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import khmt.lvtn.dhbk.model.CommonModel;
import khmt.lvtn.dhbk.model.EntityModel;
import khmt.lvtn.dhbk.model.SentimentModel;

public class GetNeedWord {
	// Get Adjective

	public ArrayList<SentimentModel> getAdj(String sen) {
		ArrayList<SentimentModel> modelAdj = new ArrayList<SentimentModel>();
		sen = sen.toLowerCase();
		int location = 0;
		float score1;
		float score2 = 2;
		String[] oneWord = getOneWord(sen);
		String file = "data/tinhtu.txt";
		int dem = 0;
		for (int i = 0; i < oneWord.length; i++) {
			if (i == 0) {
				location += oneWord[i].length();
			} else {
				location = location + oneWord[i].length() + 1;
			}
			score1 = getScoreAdj(oneWord[i], file);
			if (i < oneWord.length - 1) {
				score2 = getScoreAdj(
						oneWord[i].concat(" ").concat(oneWord[i + 1]), file);
			}
			if (score1 != 2) {
				// modelAdj.add(new SentimentModel(oneWord[i], location
				// - oneWord[i].length(), score1));

				boolean flagCom = false;
				if (modelAdj.size() > 0) {
					String[] splitLast = modelAdj.get(modelAdj.size() - 1)
							.getWordSentiment().split(" ");
					if (splitLast.length >= 2) {
						if (splitLast[1].equals(oneWord[i])
								&& splitLast[0].equals(oneWord[i - 1])) {
							flagCom = true;
						}

					}
				}
				if (!flagCom) {
					modelAdj.add(new SentimentModel(oneWord[i], location
							- oneWord[i].length(), score1));
				}
			}
			if (score2 != 2) {
				modelAdj.add(new SentimentModel(oneWord[i].concat(" ").concat(
						oneWord[i + 1]), location - oneWord[i].length(), score2));
				dem = modelAdj.size();
				if (modelAdj.size() > 1
						&& modelAdj.get(dem - 1).getLocation() == modelAdj.get(
								dem - 2).getLocation()) {
					modelAdj.remove(dem - 2);
				}
				score2 = 2;
			}
		}

		return modelAdj;
	}

	// done

	// Get verb

	public ArrayList<SentimentModel> getVerb(String sen) {
		ArrayList<SentimentModel> modelVerb = new ArrayList<SentimentModel>();
		sen = sen.toLowerCase();
		int location = 0;
		float score1;
		float score2 = 2;
		String[] oneWord = getOneWord(sen);
		String file = "data/dongtu.txt";
		int dem = 0;
		for (int i = 0; i < oneWord.length; i++) {
			if (i == 0) {
				location += oneWord[i].length();
			} else {
				location = location + oneWord[i].length() + 1;
			}
			score1 = getScoreVerb(oneWord[i], file);
			if (i < oneWord.length - 1) {
				score2 = getScoreVerb(
						oneWord[i].concat(" ").concat(oneWord[i + 1]), file);
			}
			if (score1 != 2) {
				// modelVerb.add(new SentimentModel(oneWord[i], location
				// - oneWord[i].length(), score1));
				boolean flagCom = false;
				if (modelVerb.size() > 0) {
					String[] splitLast = modelVerb.get(modelVerb.size() - 1)
							.getWordSentiment().split(" ");
					if (splitLast.length >= 2) {
						if (splitLast[1].equals(oneWord[i])
								&& splitLast[0].equals(oneWord[i - 1])) {
							flagCom = true;
						}

					}
				}
				if (!flagCom) {
					modelVerb.add(new SentimentModel(oneWord[i], location
							- oneWord[i].length(), score1));
				}
			}
			if (score2 != 2) {
				modelVerb
						.add(new SentimentModel(oneWord[i].concat(" ").concat(
								oneWord[i + 1]),
								location - oneWord[i].length(), score2));
				dem = modelVerb.size();
				if (modelVerb.size() > 1
						&& modelVerb.get(dem - 1).getLocation() == modelVerb
								.get(dem - 2).getLocation()) {
					modelVerb.remove(dem - 2);
				}
				score2 = 2;
			}
		}

		return modelVerb;
	}

	// done

	// Get Auxiliary
	public ArrayList<SentimentModel> getAuxiliary(String sen) {
		sen = sen.toLowerCase();
		int location = 0;
		float score1;
		float score2 = 2;
		String[] oneWord = getOneWord(sen);
		String file = "data/trotu.txt";
		int dem = 0;
		ArrayList<SentimentModel> modelAuxiliary = new ArrayList<SentimentModel>();
		for (int i = 0; i < oneWord.length; i++) {
			if (i == 0) {
				location += oneWord[i].length();
			} else {
				location = location + oneWord[i].length() + 1;
			}
			score1 = getScoreAuxiliary(oneWord[i], file);
			if (i < oneWord.length - 1) {
				score2 = getScoreAuxiliary(
						oneWord[i].concat(" ").concat(oneWord[i + 1]), file);
			}
			if (score1 != 2) {
				modelAuxiliary.add(new SentimentModel(oneWord[i], location
						- oneWord[i].length(), score1));
				dem++;
			}
			if (score2 != 2) {
				modelAuxiliary.add(new SentimentModel(oneWord[i]
						.concat(oneWord[i + 1]),
						location - oneWord[i].length(), score2));
				dem++;
				if (modelAuxiliary.size() > 1
						&& modelAuxiliary.get(dem - 1).getLocation() == modelAuxiliary
								.get(dem - 2).getLocation()) {
					modelAuxiliary.remove(dem - 2);
					dem--;
				}
				score2 = 2;
			}
		}

		return modelAuxiliary;
	}

	// done

	// Get Auxiliary
	// public ArrayList<CommonModel> getAuxiliary(String sen) {
	// ArrayList<CommonModel> modelAuxiliary = new ArrayList<CommonModel>();
	// sen = sen.toLowerCase();
	// int location = 0;
	// String[] oneWord = getOneWord(sen);
	// String file = "data/trotu.txt";
	// int dem = 0;
	// boolean flag1 = false;
	// boolean flag2 = false;
	// for (int i = 0; i < oneWord.length; i++) {
	// if (i == 0) {
	// location += oneWord[i].length();
	// } else {
	// location = location + oneWord[i].length() + 1;
	// }
	// flag1 = compareWord(oneWord[i], file);
	// if (i < oneWord.length - 1) {
	// flag2 = compareWord(
	// oneWord[i].concat(" ").concat(oneWord[i + 1]), file);
	// }
	//
	// if (flag1) {
	// modelAuxiliary.add(new CommonModel(oneWord[i], location
	// - oneWord[i].length()));
	// }
	//
	// if (flag2) {
	// modelAuxiliary
	// .add(new CommonModel(oneWord[i].concat(" ").concat(
	// oneWord[i + 1]), location - oneWord[i].length()));
	// dem = modelAuxiliary.size();
	// if (modelAuxiliary.size() > 1
	// && modelAuxiliary.get(dem - 1).getLocation() == modelAuxiliary
	// .get(dem - 2).getLocation()) {
	// modelAuxiliary.remove(dem - 2);
	// }
	// flag2 = false;
	// }
	// }
	//
	// return modelAuxiliary;
	// }

	// done

	// Get Entity
	public ArrayList<CommonModel> getEntity(String sen) {
		ArrayList<CommonModel> modelEntity = new ArrayList<CommonModel>();
		sen = sen.toLowerCase();
		int location = 0;
		String[] oneWord = getOneWord(sen);
		String file = "data/thucthe.txt";
		int dem = 0;
		boolean flag1 = false;
		boolean flag2 = false;
		for (int i = 0; i < oneWord.length; i++) {
			if (i == 0) {
				location += oneWord[i].length();
			} else {
				location = location + oneWord[i].length() + 1;
			}
			flag1 = compareWord(oneWord[i], file);
			if (i < oneWord.length - 1) {
				flag2 = compareWord(
						oneWord[i].concat(" ").concat(oneWord[i + 1]), file);
			}

			if (flag1) {
				// modelEntity.add(new CommonModel(oneWord[i], location
				// - oneWord[i].length()));

				boolean flagCom = false;
				if (modelEntity.size() > 0) {
					String[] splitLast = modelEntity
							.get(modelEntity.size() - 1).getWordCommon()
							.split(" ");
					if (splitLast.length >= 2) {
						if (splitLast[1].equals(oneWord[i])
								&& splitLast[0].equals(oneWord[i - 1])) {
							flagCom = true;
						}

					}
				}
				if (!flagCom) {
					modelEntity.add(new CommonModel(oneWord[i], location
							- oneWord[i].length()));
				}
			}

			if (flag2) {
				modelEntity.add(new CommonModel(oneWord[i].concat(" ").concat(
						oneWord[i + 1]), location - oneWord[i].length()));
				dem = modelEntity.size();
				if (modelEntity.size() > 1
						&& modelEntity.get(dem - 1).getLocation() == modelEntity
								.get(dem - 2).getLocation()) {
					modelEntity.remove(dem - 2);
				}
				flag2 = false;
			}
		}

		return modelEntity;
	}

	// done

	// Get Property
	public ArrayList<CommonModel> getProperty(String sen) {
		ArrayList<CommonModel> modelProperty = new ArrayList<CommonModel>();
		sen = sen.toLowerCase();
		int location = 0;
		String[] oneWord = getOneWord(sen);
		String file = "data/thuoctinh.txt";
		int dem = 0;
		boolean flag1 = false;
		boolean flag2 = false;
		boolean flag3 = false;
		for (int i = 0; i < oneWord.length; i++) {
			if (i == 0) {
				location += oneWord[i].length();
			} else {
				location = location + oneWord[i].length() + 1;
			}
			flag1 = compareWord(oneWord[i], file);
			if (i < oneWord.length - 1) {
				flag2 = compareWord(
						oneWord[i].concat(" ").concat(oneWord[i + 1]), file);
			}

			if (i < oneWord.length - 2) {
				flag3 = compareWord(
						oneWord[i].concat(" ").concat(oneWord[i + 1])
								.concat(" ").concat(oneWord[i + 2]), file);
			}

			if (flag1) {
				// modelProperty.add(new CommonModel(oneWord[i], location
				// - oneWord[i].length()));
				boolean flagCom = false;
				if (modelProperty.size() > 0) {
					String[] splitLast = modelProperty
							.get(modelProperty.size() - 1).getWordCommon()
							.split(" ");
					if (splitLast.length >= 2) {
						if (splitLast[1].equals(oneWord[i])
								&& splitLast[0].equals(oneWord[i - 1])) {
							flagCom = true;
						}

					}
				}
				if (!flagCom) {
					modelProperty.add(new CommonModel(oneWord[i], location
							- oneWord[i].length()));
				}

			}

			if (flag2) {
				modelProperty
						.add(new CommonModel(oneWord[i].concat(" ").concat(
								oneWord[i + 1]), location - oneWord[i].length()));
				dem = modelProperty.size();
				if (modelProperty.size() > 1
						&& modelProperty.get(dem - 1).getLocation() == modelProperty
								.get(dem - 2).getLocation()) {
					modelProperty.remove(dem - 2);
				}
				flag2 = false;
			}

			if (flag3) {
				modelProperty
						.add(new CommonModel(oneWord[i].concat(" ")
								.concat(oneWord[i + 1]).concat(" ")
								.concat(oneWord[i + 2]), location
								- oneWord[i].length()));
				dem = modelProperty.size();
				if (modelProperty.size() > 1
						&& modelProperty.get(dem - 1).getLocation() == modelProperty
								.get(dem - 2).getLocation()) {
					modelProperty.remove(dem - 2);
				}
				flag3 = false;
			}
		}

		return modelProperty;
	}

	// done

	// get Entity and Property
	public ArrayList<CommonModel> getEntityAndProperty(String sen) {
		sen = sen.toLowerCase();
		EntityModel list = new EntityModel();
		list = getListEntityAndProperty(sen);
		ArrayList<CommonModel> model = (ArrayList<CommonModel>) list
				.getSuperEntity();
		ArrayList<CommonModel> model1 = (ArrayList<CommonModel>) list
				.getProperty();
		for (int i = 0; i < model1.size(); i++) {
			model.add(new CommonModel(model1.get(i).getWordCommon(), model1
					.get(i).getLocation()));
		}
		Collections.sort(model, new Comparator<CommonModel>() {

			@Override
			public int compare(CommonModel o1, CommonModel o2) {
				// TODO Auto-generated method stub
				return o1.getLocation() - o2.getLocation();
			}
		});
		return model;
	}

	// get Entity
	public EntityModel getListEntityAndProperty(String sen) {
		return (new EntityModel(getEntity(sen), getProperty(sen)));
	}

	// done

	// Get Pronoun
	public ArrayList<CommonModel> getPronoun(String sen) {
		ArrayList<CommonModel> modelPronoun = new ArrayList<CommonModel>();
		sen = sen.toLowerCase();
		int location = 0;
		String[] oneWord = getOneWord(sen);
		String file = "data/daitu.txt";
		int dem = 0;
		boolean flag1 = false;
		boolean flag2 = false;
		boolean flag3 = false;
		for (int i = 0; i < oneWord.length; i++) {
			if (i == 0) {
				location += oneWord[i].length();
			} else {
				location = location + oneWord[i].length() + 1;
			}
			flag1 = compareWord(oneWord[i], file);
			if (i < oneWord.length - 1) {
				flag2 = compareWord(
						oneWord[i].concat(" ").concat(oneWord[i + 1]), file);
			}

			if (i < oneWord.length - 2) {
				flag3 = compareWord(
						oneWord[i].concat(" ").concat(oneWord[i + 1])
								.concat(" ").concat(oneWord[i + 2]), file);
			}

			if (flag1) {
				// modelPronoun.add(new CommonModel(oneWord[i], location
				// - oneWord[i].length()));
				boolean flagCom = false;
				if (modelPronoun.size() > 0) {
					String[] splitLast = modelPronoun
							.get(modelPronoun.size() - 1).getWordCommon()
							.split(" ");
					if (splitLast.length >= 2) {
						if (splitLast[1].equals(oneWord[i])
								&& splitLast[0].equals(oneWord[i - 1])) {
							flagCom = true;
						}

					}
				}
				if (!flagCom) {
					modelPronoun.add(new CommonModel(oneWord[i], location
							- oneWord[i].length()));
				}

			}

			if (flag2) {
				modelPronoun.add(new CommonModel(oneWord[i].concat(" ").concat(
						oneWord[i + 1]), location - oneWord[i].length()));
				dem = modelPronoun.size();
				if (modelPronoun.size() > 1
						&& modelPronoun.get(dem - 1).getLocation() == modelPronoun
								.get(dem - 2).getLocation()) {
					modelPronoun.remove(dem - 2);
				}
				flag2 = false;
			}

			if (flag3) {
				modelPronoun
						.add(new CommonModel(oneWord[i].concat(" ")
								.concat(oneWord[i + 1]).concat(" ")
								.concat(oneWord[i + 2]), location
								- oneWord[i].length()));
				dem = modelPronoun.size();
				if (modelPronoun.size() > 1
						&& modelPronoun.get(dem - 1).getLocation() == modelPronoun
								.get(dem - 2).getLocation()) {
					modelPronoun.remove(dem - 2);
				}
				flag3 = false;
			}
		}

		return modelPronoun;
	}

	// done

	// Get Negative
	public ArrayList<CommonModel> getNegative(String sen) {
		ArrayList<CommonModel> modelNegative = new ArrayList<CommonModel>();
		sen = sen.toLowerCase();
		int location = 0;
		String[] oneWord = getOneWord(sen);
		String file = "data/phudinh.txt";
		int dem = 0;
		boolean flag1 = false;
		boolean flag2 = false;
		for (int i = 0; i < oneWord.length; i++) {
			if (i == 0) {
				location += oneWord[i].length();
			} else {
				location = location + oneWord[i].length() + 1;
			}
			flag1 = compareWord(oneWord[i], file);
			if (i < oneWord.length - 1) {
				flag2 = compareWord(
						oneWord[i].concat(" ").concat(oneWord[i + 1]), file);
			}

			if (flag1) {
				modelNegative.add(new CommonModel(oneWord[i], location
						- oneWord[i].length()));
			}

			if (flag2) {
				modelNegative
						.add(new CommonModel(oneWord[i].concat(" ").concat(
								oneWord[i + 1]), location - oneWord[i].length()));
				dem = modelNegative.size();
				if (modelNegative.size() > 1
						&& modelNegative.get(dem - 1).getLocation() == modelNegative
								.get(dem - 2).getLocation()) {
					modelNegative.remove(dem - 2);
				}
				flag2 = false;
			}
		}

		return modelNegative;
	}

	// done

	// Compare word

	public boolean compareWord(String string, String file) {
		boolean flag = false;

		try {
			FileReader fr = new FileReader(file);
			BufferedReader br = new BufferedReader(fr);
			String line = null;
			while ((line = br.readLine()) != null) {
				if (string.compareTo(line) == 0) {
					flag = true;
					break;
				}
			}
			fr.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println("Exception" + e);
		}

		return flag;
	}

	// done

	// Get score Adjective

	private float getScoreAdj(String string, String file) {
		float score = 2;
		String tagForWord = null;
		String tagForScore = null;

		tagForWord = "<adj>";
		tagForScore = "<p>";
		try {
			FileReader fr = new FileReader(file);
			BufferedReader br = new BufferedReader(fr);
			String line = null;
			while ((line = br.readLine()) != null) {
				int indexOfWord = line.indexOf(tagForWord)
						+ tagForWord.length();
				int indexOfNextTag = line.indexOf(tagForScore);
				String wordOfFile = line.substring(indexOfWord, indexOfNextTag);
				if (string.compareTo(wordOfFile) == 0) {
					String endTag = "<e>";
					int indexOfScore = indexOfNextTag + tagForScore.length();
					int indexOfEndTag = line.indexOf(endTag);
					String stringScore = line.substring(indexOfScore,
							indexOfEndTag);
					score = Float.parseFloat(stringScore);
					break;
				}
			}
			fr.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println("Exception" + e);
		}

		return score;
	}

	// done

	// Get score Verb

	private float getScoreVerb(String string, String file) {
		float score = 2;
		String tagForWord = null;
		String tagForScore = null;

		tagForWord = "<verb>";
		tagForScore = "<p>";
		try {
			FileReader fr = new FileReader(file);
			BufferedReader br = new BufferedReader(fr);
			String line = null;
			while ((line = br.readLine()) != null) {
				int indexOfWord = line.indexOf(tagForWord)
						+ tagForWord.length();
				int indexOfNextTag = line.indexOf(tagForScore);
				String wordOfFile = line.substring(indexOfWord, indexOfNextTag);
				if (string.compareTo(wordOfFile) == 0) {
					String endTag = "<e>";
					int indexOfScore = indexOfNextTag + tagForScore.length();
					int indexOfEndTag = line.indexOf(endTag);
					String stringScore = line.substring(indexOfScore,
							indexOfEndTag);
					score = Float.parseFloat(stringScore);
					break;
				}
			}
			fr.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println("Exception" + e);
		}

		return score;
	}

	// done

	// Get score Verb

	private float getScoreAuxiliary(String string, String file) {
		float score = 2;
		String tagForWord = null;
		String tagForScore = null;

		tagForWord = "<aux>";
		tagForScore = "<p>";
		try {
			FileReader fr = new FileReader(file);
			BufferedReader br = new BufferedReader(fr);
			String line = null;
			while ((line = br.readLine()) != null) {
				int indexOfWord = line.indexOf(tagForWord)
						+ tagForWord.length();
				int indexOfNextTag = line.indexOf(tagForScore);
				String wordOfFile = line.substring(indexOfWord, indexOfNextTag);
				if (string.compareTo(wordOfFile) == 0) {
					String endTag = "<e>";
					int indexOfScore = indexOfNextTag + tagForScore.length();
					int indexOfEndTag = line.indexOf(endTag);
					String stringScore = line.substring(indexOfScore,
							indexOfEndTag);
					score = Float.parseFloat(stringScore);
					break;
				}
			}
			fr.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println("Exception" + e);
		}

		return score;
	}

	// done

	// Get array one word

	public String[] getOneWord(String sen) {
		sen.replace(".", "");
		return sen.split(" ");

	}

	// done

	// Get array two word
	public ArrayList<String> getTwoWordArray(String sen) {
		ArrayList<String> twoWords = new ArrayList<String>();
		String[] words = getOneWord(sen);
		if (words != null) {
			int length = words.length;
			for (int i = 0; i < length - 1; i++) {
				twoWords.add(words[i].concat(" ").concat(words[i + 1]));
			}
		}
		return twoWords;
	}

	// done

	// Get "Nhung"
	public ArrayList<Integer> getNhungWord(String sen) {
		ArrayList<Integer> listInt = new ArrayList<Integer>();
		sen = sen.toLowerCase();
		String[] oneWord = getOneWord(sen);
		int location = 0;
		for (int i = 0; i < oneWord.length; i++) {
			if (i == 0) {
				location += oneWord[i].length();
			} else {
				location = location + oneWord[i].length() + 1;
			}
			if (oneWord[i].equals("nhưng")) {
				listInt.add(location - oneWord[i].length());
			}
		}
		return listInt;
	}

}
